//
//  PrescriptionViewModel.swift
//  EyeVisionForDoctors
//
//  Created by PMC on 2020/2/29.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import Foundation
import HandyJSON

enum PrescriptionStatus: Int, HandyJSONEnum {
    case unreviewed
    case reviewpass
    case reject
    
    var statusText: String {
        switch self {
        case .unreviewed: return ""
        case .reviewpass: return "审核通过"
        case .reject: return "审核驳回"
        }
    }
    
    var statusColor: UIColor? {
        switch self {
        case .reviewpass: return .textDark
        case .reject: return UIColor(hex: "#F54C4C")
        default: return nil
        }
    }
}

struct PrescriptionModel: HandyJSON {
    var patientName: String?
    var diagnoseId: String?
    var patientId: String?
    var docId: String?
    /// 诊断类型
    var strType: String?
    var sex: String?
    var updateTime: String?
    /// 身份证号
    var idNumber: String?
    var docName: String?
    /// 病例id
    var mrId: String?
    var createTime: String?
    /// 诊断类型
    var diagType: Int = -1
    var id: String?
    var pAge: String?
    /// 审核医生
    var auditorName: String?
    
    /// 审核状态
    var status: PrescriptionStatus = .unreviewed
}

struct PrescriptionViewModel {
    var models: [PrescriptionModel]
    
    var numberOfRows: Int { return models.count }
    
    func viewModel(for indexPath: IndexPath) -> PrescriptionRowViewModel {
        return PrescriptionRowViewModel(model: models[indexPath.row])
    }
    
    mutating func loadMore(with models: [PrescriptionModel]) {
        self.models += models
    }
}

struct PrescriptionRowViewModel {
    let model: PrescriptionModel
    
    var doctorName: String { return model.docName ?? "" }
    
    var doctorId: String { return model.docId ?? ""}
    
    var prescriptionType: String { return model.strType ?? "" }
    
    var reviewDoctorName: String { return "审核医生：\(model.auditorName ?? "")" }
    
    var updateTime: String { return model.updateTime ?? "" }
    
    var createTime: String { return model.createTime ?? "" }
    
    var status: PrescriptionStatus { model.status }
    
    var patientName: String { return model.patientName ?? "" }
    
    var patientAge: String { return model.pAge ?? "" }
    
    var patientSex: String { return model.sex ?? "" }
    
    /// 处方id
    var prescriptionId: String { return model.id ?? "" }
    /// 诊断id
    var diagnoseId: String { return model.diagnoseId ?? "" }
    
    var cellHeight: CGFloat { return 80 }
}

struct PrescriptionDetailModel: HandyJSON {
    var auditor: String?
    var auditorId: String?
    var diagnoseId: String?
    /// 疾病诊断
    var diagnoseResult: String?
    var passTime: String?
    var patientCondition: String?
    var prescriptionId: String?
    var reject: String?
    /// 医生嘱托
    var remark: String?
    var status: PrescriptionStatus = .unreviewed
    var westernList: [DrugsModel]?
    var orderId: String?
    var orderStatus: OrderStatus = .all
    var preStatus: PrescriptionOrderStatus = .unreview
    
    /// 医生意见
    var advice: String?
    /// 模板名称
    var name: String?
}

struct PrescriptionDetailViewModel {
    let model: PrescriptionDetailModel
    
    var westernList: [DrugsModel] { model.westernList ?? [] }
    /// 审核医生
    var reviewDoctor: String { model.auditor ?? "" }
    var reviewDoctorId: String { model.auditorId ?? "" }
    
    /// 诊断id
    var diagnoseId: String { model.diagnoseId ?? "" }
    /// 医生意见
    var recommend: String { model.diagnoseResult ?? "" }
    /// 审核时间
    var passTime: String { model.passTime ?? "" }
    /// 诊断疾病
    var patientCondition: String { model.patientCondition ?? "" }
    /// 处方id
    var prescriptionId: String { model.prescriptionId ?? "" }
    /// 驳回理由
    var rejectReason: String { model.reject ?? "" }
    /// 医生嘱托
    var doctorUrge: String { model.remark ?? "" }
    
    /// 处方状态
    var status: PrescriptionStatus { model.status }
    
    /// 模板名称
    var templateName: String { model.name ?? "" }
    /// 医生意见
    var advice: String { model.advice ?? "" }
    /// 诊断
    var diagnoseResult: String { model.diagnoseResult ?? "" }
    
    var orderId: String { model.orderId ?? "" }
    var orderStatus: OrderStatus { model.orderStatus }
    
    var preStatus: PrescriptionOrderStatus { model.preStatus }
    
    var showEmpty: Bool { return westernList.count <= 0 }
    
    var numberOfRows: Int { return westernList.count }
    
    func viewModel(for indexPath: IndexPath) -> DrugsRowViewModel {
        return DrugsRowViewModel(model: westernList[indexPath.row])
    }
}

// MARK: - 处方模板
struct PrescriptionTemplateModel: HandyJSON {
    var createTime: String?
    var diagnoseResult: String?
    var name: String?
    var id: String?
}

struct PrescriptionTemplateViewModel {
    struct RowViewModel {
        let model: PrescriptionTemplateModel
        
        var createTime: String { return model.createTime ?? "" }
        var diagnoseResult: String { return model.diagnoseResult ?? "" }
        var templateName: String { return model.name ?? "" }
        var templateId: String { return model.id ?? "" }
    }
    
    var models: [PrescriptionTemplateModel]
    
    var numberOfRows: Int { return models.count }
    
    func viewModel(for indexPath: IndexPath) -> RowViewModel {
        return RowViewModel(model: models[indexPath.row])
    }
    
    mutating func loadMore(with models: [PrescriptionTemplateModel]) {
        self.models += models
    }
}
